//
//  SureSetPwdController.m
//  SafPay
//
//  Created by  ztryxh on 2018/6/21.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "SureSetPwdController.h"
#import "CodeView.h"
#import "CopySafeWordController.h"
#import "SettingViewController.h"
#import "HomeViewController.h"
@interface SureSetPwdController ()
@property (nonatomic,strong) TRRequest *request;
@property (nonatomic,copy) NSString *surepwd;
@end

@implementation SureSetPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"再次输入安全密码";
    [self initUI];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}
- (void)initUI
{
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 108)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UILabel *lab = [UILabel labelWithFrame:CGRectMake(12, 20, kScreenWidth - 24, 14) font:kFONT(15) color:rgb(58,58,58) text:@"再次输入新密码"];
    lab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lab];
    lab.textAlignment = NSTextAlignmentCenter;
    
    kWeakSelf(weakSelf);
    CodeView *codeView = [[CodeView alloc] initWithFrame:CGRectMake(60, lab.yzBottom + 5, kScreenWidth - 120, (kScreenWidth - 120)/6)  num:6 lineColor:[UIColor blackColor] textFont:14];
    codeView.hasSpaceLine = NO;
    //输入风格
    codeView.codeType = CodeViewTypeCustom;
    codeView.hasUnderLine = YES;
    codeView.noInputAni = NO;
    codeView.codeType = CodeViewTypeSecret;

    [view addSubview:codeView];
    [codeView beginEdit];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    btn.titleLabel.font = kFONT(16);
    btn.backgroundColor = rgb(33,177,227);
    [self.view addSubview:btn];
    
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

        [self getNetWork];
    }];
    codeView.EditingBlcok = ^(NSString *text) {
        
        if(text.length<6){
            
            [btn setUserInteractionEnabled:NO];
            [btn setBackgroundColor:kGrayColor];
        }else{
            
            [btn setUserInteractionEnabled:YES];
            [btn setBackgroundColor:rgb(33,177,227)];
        }
    };
    codeView.EndEditBlcok = ^(NSString *text) {
        
        weakSelf.surepwd=text;
        [weakSelf getNetWork];
        
    };
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)getNetWork{
    
    CopySafeWordController *vc = [CopySafeWordController new];
    Account *account=[[Account alloc]init];
    [[AccountTools shared]AddUser:account];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    return;
    
    if(![self.oldPwd isEqualToString:self.surepwd]){
        
        [MBProgressHUD showErrorWithText:@"您两次输入的密码不一致！"];
        return;
    }
    
    NSString *path;
    if(self.isChange==YES){
        
         path=[NSString stringWithFormat:@"%@%@",kBaseUrl,kImportPwd];
        
    }else{
        
         path=[NSString stringWithFormat:@"%@%@",kBaseUrl,kWalletCreate];
        
    }
   
    NSMutableDictionary *dict=[NSMutableDictionary new];
    [dict setValue:self.oldPwd forKey:@"password"];
    [dict setValue:self.surepwd forKey:@"password_confirmation"];
    if(self.isChange==YES){
        
        [dict setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];
        [dict setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    }
    [self.request Post:path parameters:dict success:^(TRRequest *request, NSDictionary *responseDict) {
    
        MyLog(@"%@",responseDict);
        if([[responseDict valueForKey:@"error"] integerValue]==0){
            
            if(self.isChange==YES){
                
                if(self.isSetting){
                    
                    [MBProgressHUD showSuccessWithText:[responseDict valueForKey:@"error_msg"]];
                    [self.navigationController.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                       
                        if([obj isKindOfClass:[SettingViewController class]]){
                            
                            [self.navigationController popToViewController:obj animated:YES];
                        }
                    }];
                    
                }else{
                    
//                    CopySafeWordController *vc = [CopySafeWordController new];
//                    vc.keyWord=[AccountTools shared].currentAccount.keyWord;
//                    vc.isSetting=self.isSetting;
                    HomeViewController *vc = [HomeViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
              
                
            }else{
                
//                [WalletStatusModel saveWalletToken:[[responseDict valueForKey:@"data"]valueForKey:@"remember_token"]];
                CopySafeWordController *vc = [CopySafeWordController new];
                vc.keyWord=[[responseDict valueForKey:@"data"]valueForKey:@"zhujici"];
                Account *account=[[Account alloc]init];
                account.token=[[responseDict valueForKey:@"data"]valueForKey:@"remember_token"];
                account.userId=[[responseDict valueForKey:@"data"]valueForKey:@"uid"];
                account.keyWord=[[responseDict valueForKey:@"data"]valueForKey:@"zhujici"];
                [[AccountTools shared]AddUser:account];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
            
        }else{

            
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
        
    } failure:^(TRRequest *request, NSError *error) {
        
        [MBProgressHUD showErrorWithText:@"网络请求失败！"];
    }];
}
-(TRRequest *)request{
    
    if(!_request){
        
        _request=[TRRequest request];
    }
    return _request;
}

@end
